package com.cyy.springbootinit.service.impl;
import java.util.Date;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.springbootinit.common.ErrorCode;
import com.cyy.springbootinit.constant.CommonConstant;
import com.cyy.springbootinit.exception.ThrowUtils;
import com.cyy.springbootinit.mapper.BuildingMapper;
import com.cyy.springbootinit.model.dto.building.BuildingQueryRequest;
import com.cyy.springbootinit.model.entity.Building;
import com.cyy.springbootinit.model.entity.User;
import com.cyy.springbootinit.model.vo.BuildingVO;
import com.cyy.springbootinit.model.vo.UserVO;
import com.cyy.springbootinit.service.BuildingService;
import com.cyy.springbootinit.service.UserService;
import com.cyy.springbootinit.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 楼宇服务实现
 *
 * @author 程序员cyy</a>
 * @from cyy</a>
 */
@Service
@Slf4j
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {

    /**
     * 校验数据
     *
     * @param building
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validBuilding(Building building, boolean add) {

        ThrowUtils.throwIf(building == null, ErrorCode.PARAMS_ERROR);
        // todo 从对象中取值
        String buildingName = building.getBuildingName();
        String address = building.getAddress();

        // 创建数据时，参数不能为空
        if (add) {
            // todo 补充校验规则
            //ThrowUtils.throwIf(StringUtils.isBlank(title), ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(StringUtils.isBlank(buildingName), ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(address==null,ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则

    }

    /**
     * 获取查询条件
     *
     * @param buildingQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Building> getQueryWrapper(BuildingQueryRequest buildingQueryRequest) {


        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        if (buildingQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Integer buildingId = buildingQueryRequest.getBuildingId();
        String buildingName = buildingQueryRequest.getBuildingName();
        String address = buildingQueryRequest.getAddress();
        String sortField = buildingQueryRequest.getSortField();
        String sortOrder = buildingQueryRequest.getSortOrder();
        // todo 补充需要的查询条件
        // 从多字段中搜索
       /* if (StringUtils.isNotBlank(searchText)) {
            // 需要拼接查询条件
            queryWrapper.and(qw -> qw.like("title", searchText).or().like("content", searchText));
        }*/
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(buildingName), "buildingName",buildingName);
        queryWrapper.like(StringUtils.isNotBlank(address), "address", address);
        // JSON 数组查询
        // 精确查询
        queryWrapper.eq(buildingId != null, "buildingId", buildingId);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取楼宇封装
     *
     * @param building
     * @param request
     * @return
     */
    @Override
    public BuildingVO getBuildingVO(Building building, HttpServletRequest request) {
        BuildingVO buildingVO = BuildingVO.objToVo(building);
        return buildingVO;
    }

    /**
     * 分页获取楼宇封装
     *
     * @param buildingPage
     * @param request
     * @return
     */
    @Override
    public Page<BuildingVO> getBuildingVOPage(Page<Building> buildingPage, HttpServletRequest request) {
        List<Building> buildingList = buildingPage.getRecords();
        Page<BuildingVO> buildingVOPage = new Page<>(buildingPage.getCurrent(), buildingPage.getSize(), buildingPage.getTotal());
        if (CollUtil.isEmpty(buildingList)) {
            return buildingVOPage;
        }
        // 对象列表 => 封装对象列表
        List<BuildingVO> buildingVOList = buildingList.stream().map(building -> {
            return BuildingVO.objToVo(building);
        }).collect(Collectors.toList());

        // todo 可以根据需要为封装对象补充值，不需要的内容可以删除
        // region 可选
        // 2. 已登录，获取用户点赞、收藏状态

        buildingVOPage.setRecords(buildingVOList);
        return buildingVOPage;
    }

}
